package com.luntan.school.web.servlet.admin;




import com.luntan.school.dao.AdminDao;
import com.luntan.school.model.Admin;
import com.luntan.school.service.AdminService;
import com.luntan.school.service.impl.AdminServicelmpl;


import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "AdminServletlmpl", value = "/admin/login")
public class AdminLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String account = request.getParameter("account");
        String password = request.getParameter("password");


        AdminService adminService = new AdminServicelmpl();
        Admin admin = adminService.login(account, password);
        if (admin != null) { //登陆成功
            request.getSession().setAttribute("admin", admin);


            String rootPath = request.getContextPath(); //获取项目根目录的绝对路径
            response.sendRedirect(rootPath + "/admin/index.jsp");
        } else { //登陆失败
            request.setAttribute("error", "账户或密码错误！");
            request.getRequestDispatcher("/admin/login.jsp").forward(request, response);
        }

    }
}
